/*
 * Created on 08/01/2013 - 2:24 PM
 */
package sembr.vtk.animation;

import vtk.*;

/**
 *
 * @author adriano
 */
public class Animation 
{    
    private static VtkMovie3D movie;
    private static VtkMovie2D movie2d;
    
    static
    {
        System.loadLibrary("vtkCommonJava");
        System.loadLibrary("vtkFilteringJava");
        System.loadLibrary("vtkIOJava");
        System.loadLibrary("vtkImagingJava");
        System.loadLibrary("vtkGraphicsJava");
        System.loadLibrary("vtkRenderingJava");
    }
 
    static class vtkTimerCallback 
    {
        public void Execute3D()
        {
            ((vtkActor)actorFieldCollection.GetItemAsObject(TimerCount)).SetVisibility(0);
            actor1 = ((vtkActor)actorFieldCollection.GetItemAsObject(TimerCount + 1));
            actor1.SetVisibility(1);

            ((vtkActor)actorPlaneCollection.GetItemAsObject(TimerCount)).SetVisibility(TimerCount);
            actor2 = ((vtkActor)actorPlaneCollection.GetItemAsObject(TimerCount + 1));
            actor2.SetVisibility(1);

            iren.GetRenderWindow().GetRenderers().GetFirstRenderer().AddActor(actor1);
            iren.GetRenderWindow().GetRenderers().GetFirstRenderer().AddActor(actor2);
                        
            iren.GetRenderWindow().GetRenderers().GetFirstRenderer().ResetCamera();
            iren.GetRenderWindow().Render();
            
            ++this.TimerCount;
            System.out.println(this.TimerCount);          
        }
        
        public void Execute2D()
        {
            ((vtkActor)actorFieldCollection.GetItemAsObject(TimerCount)).SetVisibility(0);
            actor1 = ((vtkActor)actorFieldCollection.GetItemAsObject(TimerCount + 1));
            actor1.SetVisibility(1);

            iren.GetRenderWindow().GetRenderers().GetFirstRenderer().AddActor(actor1);            
                        
            iren.GetRenderWindow().GetRenderers().GetFirstRenderer().ResetCamera();
            iren.GetRenderWindow().Render();
            
            ++this.TimerCount;
            System.out.println(this.TimerCount); 
        }
 
        private int TimerCount = 0;
        private vtkActor actor1;
        private vtkActor actor2;
        public vtkActorCollection actorFieldCollection;
        public vtkActorCollection actorPlaneCollection;
        public vtkRenderWindowInteractor iren;
    }
 
    public Animation(VtkMovie3D movie)
    {
        Animation.movie = movie;       
          
        vtkRenderer renderer = new vtkRenderer();
        vtkRenderWindow renderWindow = new vtkRenderWindow();
        renderWindow.AddRenderer(renderer);
        vtkRenderWindowInteractor renderWindowInteractor = new vtkRenderWindowInteractor();
        renderWindowInteractor.SetRenderWindow(renderWindow);

        // Add the actor to the scene
        ((vtkActor)movie.getActorFieldCollection().GetItemAsObject(0)).SetVisibility(1);
        ((vtkActor)movie.getActorPlaneCollection().GetItemAsObject(0)).SetVisibility(1);
        renderer.AddActor((vtkActor)movie.getActorFieldCollection().GetItemAsObject(0));
        renderer.AddActor((vtkActor)movie.getActorPlaneCollection().GetItemAsObject(0));
        renderer.AddActor(movie.getBarActor());
        
        //vtkTransform transformation = new vtkTransform();
        //transformation.PostMultiply();
        //transformation.RotateY(180.);
        vtkCamera camera1 = new vtkCamera();
        camera1.SetViewUp(0.00, 1.00, 0.00);
        camera1.SetPosition(2, 2, 2);
        camera1.SetFocalPoint(0, 0, 0);  
        //camera1.ApplyTransform(transformation);
        
        renderer.SetActiveCamera(camera1);
        renderer.ResetCamera();
        renderWindow.SetSize(1090, 570);
        renderWindow.SetPosition(264, 124);
        //renderWindow.SetWindowName("Filme 3D");
        renderWindow.BordersOff();
        //renderWindow.OffScreenRenderingOn();
        // Render and interact
        renderWindow.Render();
              
        renderWindowInteractor.Initialize();
        vtkTimerCallback cb = new vtkTimerCallback();
        cb.actorFieldCollection = Animation.movie.getActorFieldCollection();
        cb.actorPlaneCollection = Animation.movie.getActorPlaneCollection();
        cb.iren = renderWindowInteractor;        
         
        // Sign up to receive TimerEvent  
        renderWindowInteractor.AddObserver("TimerEvent", cb, "Execute3D");  
        //renderWindowInteractor.AddObserver("DestroyTimerEvent", cb, "Finish");
        int nrOfFrames = Animation.movie.getNrOfFrames();
        renderWindowInteractor.CreateRepeatingTimer(nrOfFrames - 2);  
        // start the interaction and timer        
        renderWindowInteractor.Start();  
    } 
    
    public Animation(VtkMovie2D movie2D)
    {
        Animation.movie2d = movie2D;
        
        vtkRenderer renderer = new vtkRenderer();
        vtkRenderWindow renderWindow = new vtkRenderWindow();
        renderWindow.AddRenderer(renderer);
        vtkRenderWindowInteractor renderWindowInteractor = new vtkRenderWindowInteractor();
        renderWindowInteractor.SetRenderWindow(renderWindow);

        // Add the actor to the scene
        ((vtkActor)Animation.movie2d.getActorFieldCollection().GetItemAsObject(0)).SetVisibility(1);
        renderer.AddActor((vtkActor)Animation.movie2d.getActorFieldCollection().GetItemAsObject(0));
        renderer.AddActor(Animation.movie2d.getBarActor());
       
//        vtkCamera camera1 = new vtkCamera();
//        camera1.SetViewUp(0.00, 1.00, 0.00);
//        camera1.SetPosition(2, 2, 2);
//        camera1.SetFocalPoint(0, 0, 0);  
                
        //renderer.SetActiveCamera(camera1);
        renderer.ResetCamera();
        renderWindow.SetSize(1090, 570);
        renderWindow.SetPosition(264, 124);
        renderWindow.BordersOn();
        // Render and interact
       // renderWindow.Render();
                     
        vtkTimerCallback cb = new vtkTimerCallback();
        cb.actorFieldCollection = Animation.movie2d.getActorFieldCollection();
        cb.iren = renderWindowInteractor;        
         
        renderWindowInteractor.Initialize();
        // Sign up to receive TimerEvent  
        renderWindowInteractor.AddObserver("TimerEvent", cb, "Execute2D");  
        int nrOfFrames = Animation.movie2d.getNrOfFrames();
        renderWindowInteractor.CreateRepeatingTimer(nrOfFrames - 2);  
        // start the interaction and timer     
        renderWindowInteractor.Start();
    }
}
